//
//  HomeViewController.m
//  XKBaseDemo
//
//  Created by xukai on 2017/10/20.
//  Copyright © 2017年 xukai. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCellModel.h"
#import "HomeCell.h"
#import "DetaileController.h"

@interface HomeViewController ()<UITableViewDelegate,UITableViewDataSource, HomeCellDelegate, DetaileDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation HomeViewController

static NSString *ID = @"HOMECELL";

-(NSArray *) datas
{
    if(!_datas)
    {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ZhuBoList.json" ofType:nil];
        NSData *data = [[NSFileManager defaultManager] contentsAtPath:filePath];
        NSError *error;
        id json = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:&error];
        _datas = [HomeCellModel mj_objectArrayWithKeyValuesArray:json];
        if(!_datas)
        {
             _datas = [NSArray array];
        }
       
    }
    return _datas;
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    [self setupUI];
}

-(void) setupUI
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height) style:(UITableViewStylePlain)];
    _tableView.backgroundColor = ColorFromRGB(0xeeeeee);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[HomeCell class] forCellReuseIdentifier:ID];

    [self.view addSubview:_tableView];
    
}

#pragma mark - TabView
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 420;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCellModel *model = self.datas[indexPath.row];
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.delegate = self;
    cell.model = model;
    return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    HomeCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    [cell conTouchBegin];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
        CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
        HomeCellModel *model = self.datas[indexPath.row];
        
        
        DetaileController *vc = [[DetaileController alloc] init];
        vc.model = model;
        vc.rect = rect;
        vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentViewController:vc animated:YES completion:nil];
//    });
    
    
}

-(void) homeCellTap:(HomeCellModel *)model
{
    
    DetaileController *vc = [[DetaileController alloc] init];
    vc.model = model;
    vc.delegate = self;
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:vc animated:YES completion:nil];
}

-(void) closeWithRect:(CGRect)rect
{
    
}

@end
